package com.example.edu.controller;


import java.util.List;


import java.util.Map;

import org.apache.commons.collections4.map.HashedMap;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

import com.example.edu.entity.excel.CoursePublishInfo;
import com.example.edu.entity.excel.CourseVoco;
import com.example.edu.service.CourseService;
import com.example.utils.CourseQuery;
import com.example.utils.Result;
import com.example.utils.order.OrderCourse;

/**
 * <p>
 * 课程 前端控制器
 * </p>
 *
 * @author testjava
 * @since 2020-07-02
 */
@RestController
@RequestMapping("/edu")
public class CourseController {
	
	@Autowired
	private CourseService courseService;

	
	@PostMapping("course")
	public Result saveCourse(@RequestBody CourseVoco courseVoco) {
		System.out.println(courseVoco);
		String id = courseService.saveCourse(courseVoco);
		System.out.println("bbb");
		return Result.ok().data("courseId", id);
		
	}
	
	@GetMapping("course/{id}")
	public Result getCourseVoco(@PathVariable String id) {
		
		CourseVoco courseVoco = courseService.getCourseVoco(id);
		
		return Result.ok().data("coursevoco", courseVoco);
	}
	
	@PutMapping("course")
	public Result updateCourseVoco(@RequestBody CourseVoco courseVoco) {
		System.out.println("执行了put请求");
		courseService.updateCourseVoco(courseVoco);
		
		return Result.ok();
	}
	
	@GetMapping("course/publish/{id}")
	public Result getCoursePublishInfo(@PathVariable String id) {
		
		CoursePublishInfo coursePublishInfo = courseService.getCoursePublishInfo(id);
		
		return Result.ok().data("coursePublish", coursePublishInfo);
	}
	

	
	@GetMapping("course/publish")
	public Result getAllCoursePublishInfo() {
		System.out.println("jinrugaifangfa");
		List<CoursePublishInfo> courseAllPublishInfo = courseService.getAllCoursePublishInfo();
		
		return Result.ok().data("courseAllPublishInfo", courseAllPublishInfo);
	}
	
	@PostMapping("course/publish/{current}/{size}")
	public Result getAllCoursePublishInfoByMap(@PathVariable Integer current,@PathVariable Integer size, @RequestBody(required = false) CourseQuery courseQuery) {
		
		Map<String,Object> map = new HashedMap<>();
		map.put("current", current);
		map.put("size", size);
		String title = courseQuery.getTitle();
		if(!StringUtils.isEmpty(title)) {
			map.put("title", title);
		};
		System.out.println(title);
		String status = courseQuery.getStatus();
		if(!StringUtils.isEmpty(status)) {
			map.put("status", status);
		};
		System.out.println(status);
		
		List<CoursePublishInfo> courseAllPublishInfo = courseService.getAllCoursePublishInfoByMap(map);
		
		Integer total = courseService.selectCount(map);
		Integer count = 15;

		
		return Result.ok().data("courseAllPublishInfo", courseAllPublishInfo).data("total", total).data("count", count);
	}
	
	@DeleteMapping("course/publishcourse/{courseId}")
	public Result EditCoursePublishInfo(@PathVariable String courseId) {
		courseService.deleteCoursePublishInfo(courseId);
		
		return Result.ok();
	}
	
	@PutMapping("course/publishcourse/{id}")
	public Result publishCourse(@PathVariable String id) {
	
		courseService.publishCourse(id);
		
		return Result.ok();
	}
	

}

